from datetime import datetime
from sqlalchemy.orm import sessionmaker
from shiyanlou.models import Course, User, engine, Repository
from shiyanlou.items import CourseItem, UserItem, RepositoryItem


class ShiyanlouPipeline(object):

    def process_item(self, item, spider):
        if isinstance(item, CourseItem):
            self._process_course_item(item)
        elif isinstance(item, RepositoryItem):
            self._process_rep_item(item)
        else:
            self._process_user_item(item)
        return item

    def _process_course_item(self, item):
        item['students'] = int(item['students'])
        self.session.add(Course(**item))

    def _process_user_item(self, item):
        item['level'] = int(item['level'][1:])
        item['join_date'] = datetime.strptime(item['join_date'].split()[0], '%Y-%m-%d').date()
        item['learn_courses_num'] = int(item['learn_courses_num'])
        self.session.add(User(**item))
    
    def _process_rep_item(self, item):
        item['id'] = int(item['id'])
        item['update_time'] = datetime.strptime(item['update_time'].split()[0], '%Y-%m-%dT%H:%M:%SZ')
        item['commits'] = int(item['commits'])
        item['branches'] = int(item['branches'])
        item['releases'] = int(item['releases'])
        self.session.add(Repository(**item))

    def open_spider(self, spider):
        Session = sessionmaker(bind=engine)
        self.session = Session()

    def close_spider(self, spider):
        self.session.commit()
        self.session.close()

